package com.reminiscence.main;

import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ProfiloCompleto extends Activity implements OnClickListener
{

	TextView txtNome;
	TextView txtCognome;
	TextView txtInfo;
	TextView txtData;
	TextView txtSitSen;
	TextView txtEmail;
	TextView txtCittaNatale;
	TextView txtResidenza;
	TextView txtProfPrinc;
	TextView txtProfAt;
	ImageView imgFoto;
	Bitmap bitmap;
	
	Button btnStorie;
	Button btnInsStoria;
	Button btnCercaAmici;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profilocompleto);
		MainActivity.utente.leggiUtente(this);
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		
		btnStorie = (Button)findViewById(R.id.btnVisualizzaStorie);
		btnInsStoria = (Button)findViewById(R.id.btnInsStoria);
		btnCercaAmici = (Button)findViewById(R.id.btnCercaAmici);
		
		txtNome = (TextView)findViewById(R.id.txtPCNome);
		txtCognome = (TextView)findViewById(R.id.txtPCCognome);
		txtInfo = (TextView)findViewById(R.id.txtPCInfo);
		txtData = (TextView)findViewById(R.id.txtDataUt);
		txtSitSen = (TextView)findViewById(R.id.txtSitSenUt);
		txtEmail = (TextView)findViewById(R.id.txtPCEMailUt);
		txtCittaNatale = (TextView)findViewById(R.id.txtNatoAUt);
		txtResidenza = (TextView)findViewById(R.id.txtResidenzaUt);
		txtProfPrinc = (TextView)findViewById(R.id.txtProfPrincUt);
		txtProfAt = (TextView)findViewById(R.id.txtProfAtUt);
		imgFoto = (ImageView)findViewById(R.id.imgFoto);
		
		txtNome.setText(MainActivity.utente.nome);
		txtCognome.setText(MainActivity.utente.cognome);
		txtInfo.setText(MainActivity.utente.info);
		txtData.setText(MainActivity.utente.dataN);
		txtSitSen.setText(MainActivity.utente.statoCivile);
		txtEmail.setText(MainActivity.utente.eMail);
		txtCittaNatale.setText(MainActivity.utente.luogoN);
		txtResidenza.setText(MainActivity.utente.residenza);
		txtProfPrinc.setText(MainActivity.utente.profPrinc);
		txtProfAt.setText(MainActivity.utente.profAt);
		
		String nomeFoto = MainActivity.utente.nome + "." + MainActivity.utente.cognome + ".jpg"; 
		new DownloadImageTask(imgFoto).execute("ftp://a4151578:Progetto1@remin.site90.com/public_html/foto/" + nomeFoto);
		
		btnStorie.setOnClickListener(this);
		btnInsStoria.setOnClickListener(this);
		btnCercaAmici.setOnClickListener(this);
	}

	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.menuprofilocompleto, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		if(item.getItemId() == R.id.mnuDisconnessione)
		{
			
			MainActivity.utente.logOut(this);
    		Intent i = new Intent(this, MainActivity.class);
			i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			
		}
		
		if(item.getItemId() == R.id.mnuModificaProf){
			
			Intent j = new Intent(this, ModificaProfilo.class);
			j.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(j);
			
		}
		
		return false;
	}

	@Override
	public void onClick(View arg0)
	{
			if(arg0.getId() == R.id.btnInsStoria)
			{
				Intent j = new Intent(this, InsertStoria.class);
				j.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(j);
			}
			if(arg0.getId() == R.id.btnVisualizzaStorie)
			{
				
				Intent j = new Intent(this, ListaStorie.class);
				j.putExtra("idUtente", MainActivity.utente.nomeUtente);
				j.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(j);
			}
			
			if(arg0.getId() == R.id.btnCercaAmici)
			{
				Intent i = new Intent(this, RicercaUtente.class);
	    		i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	    		startActivity(i);
			}
	}
	
	@Override
	public void onDestroy()
	{
		super.onDestroy();
		Log.i("INFO: ", "ondes");
		MainActivity.utente.logOut(null);
	}
	
	@Override
    public void onBackPressed()
	{

       return;
    }
	
	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
	{
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage)
		{
		    this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls)
		{
		    String urldisplay = urls[0];
		    Bitmap mIcon11 = null;
		    try
		    {
		        InputStream in = new java.net.URL(urldisplay).openStream();
		        mIcon11 = BitmapFactory.decodeStream(in);
		    } catch (Exception e)
		    {
		        Log.e("Error", e.getMessage());
		        e.printStackTrace();
		    }
		    return mIcon11;
		}

		protected void onPostExecute(Bitmap result)
		{
		    bmImage.setImageBitmap(result);
		}
	}
}
